% function estima_out_tabs 
% =========================================================================
% ESTIMA_OUT_TABS creates 
% 1) OUTCELL  which has all info 
% 2) TOP MODE which stores max value, and saves it in directory 
% 3) SAVEPATH which is the name of the 
% 4) TOPCELL which has model identifiers, e.g. spec, subf, outfolder 
% =========================================================================
%% 4. Create Output TABS 
%
%% 4.1 ESTPAR_TAB_D Output with the ordered modes 
% -------------------------------------------------------------------------
nstrvals=size(parmodemat,2);
estpar_tab_d=emptycell(numpar+7,nstrvals+1);
estpar_tab_d(1:numpar,1)=parnames;
estpar_tab_d(numpar+2:end,1)={'logPosterior';'logLikelihood';'Number';'Iteration count';'exitflag';'minutes'};
estpar_tab_d(1:numpar,2:end)=num2cprec(parModeMat,10);
estpar_tab_d(numpar+2,2:end)=num2cprec(logPostMat(:,2)',3);
estpar_tab_d(numpar+3,2:end)=num2cprec(logLikelMat(:,2)',3);
estpar_tab_d(numpar+4,2:end)=num2cprec(orderDescend',1);
estpar_tab_d(numpar+5:numpar+7, 2:end)=num2cprec(optimMat); 

%% 4.2 SS_TAB_D Output with associated steady states 
% -------------------------------------------------------------------------
if isunix==0
    ssTable=writeSSTable(funcmod,parModeMat,solveopt,addsol);
end
% =========================================================================

%% 4.3 DATAINFO_TAB Information about data used in estimation 
% -------------------------------------------------------------------------
datainfo_tab = emptycell(7, nstrvals + 1); 
datainfo_tab(1, 1:2) = {'data file', data_fname};
datainfo_tab(2, 1:2) = {'data path', data_path}; 
datainfo_tab(3, 1:2) = {'data range', [num2str(dataopt.start), ' to ', num2str(dataopt.finish)]}; 
if isfield(dataopt,'startest')==true
    datainfo_tab(4, 1:2) = {'data estimated', [num2str(dataopt.startest), ' to ', num2str(dataopt.finishest)]};
else
    datainfo_tab(4, 1:2) = {'data estimated', [num2str(dataopt.starest), ' to ', num2str(dataopt.finishest)]};    
end
datainfo_tab(5, 1:2) = {'demean', num2str(dataopt.demean)};
datainfo_tab(6, 1:2) = {'demean type', num2str(dataopt.demean_type)};

%% 4.4 DATASUM_TAB Names of series 
% -------------------------------------------------------------------------
datasum_tab = emptycell(nstrvals+1, size(Y, 2) + 1); 
datasum_tab{1, 1} = 'Series Names'; 
datasum_tab(1, 2:end) = Ynames; 
datasum_tab{2, 1} = 'mean';
datasum_tab(2, 2:end) = num2cprec(nanmean(Y));
datasum_tab{3, 1} = 'std dev';
datasum_tab(3, 2:end) = num2cprec(nanstd(Y));
datasum_tab = datasum_tab'; 

%% 4.5 FUNCINFO_TAB Information about solution 
% -------------------------------------------------------------------------
funcinfo_tab = emptycell(4, 2); 
funcinfo_tab(1, 1:2) = {'model', func2str(funcmod)}; 
funcinfo_tab(2, 1:2) = {'TolFun', num2str(solveopt.TolFun)};
funcinfo_tab(3, 1:2) = {'MaxIter', num2str(solveopt.MaxIter)};
funcinfo_tab(4, 1:2) = {'MaxFunEvals', num2str(solveopt.MaxFunEvals)}; 

%% 4.6 ESTINFO_TAB Information about optimization procedure 
estinfo_tab = emptycell(5, 2); 
eststr=caseEstima; 
% switch case_estima
%     case 1
%         eststr = 'CSMINWEL';
%     case 2
%         eststr = 'FMINCON'; 
%     case 3
%         eststr = 'Giorgio SA'; 
%     case 4
%         eststr = 'Simulated Annealing'; 
%     case 5
%         eststr = 'FMINSEARCH'; 
% end 
estinfo_tab(1, 1:2) = {'outpath', outpath}; 
estinfo_tab(2, 1:2) = {'routine', eststr}; 
estinfo_tab(3, 1:2) = {'TolFun', num2str(estimopt.TolFun)}; 
estinfo_tab(4, 1:2) = {'MaxIter', num2str(estimopt.MaxIter)}; 
estinfo_tab(5, 1:2) = {'MaxFunEvals', num2str(estimopt.MaxFunEvals)}; 
% =========================================================================

%% 4.7 Write OUTCELL 
mod_est_info_tab = merge_cells(estinfo_tab, funcinfo_tab, 2); 
outcell = merge_cells(mod_est_info_tab, datainfo_tab, 2); 
outcell = merge_cells(outcell, datasum_tab, 2); 
outcell = merge_cells(outcell, estpar_tab_d, 2); 
if isunix==0 && isempty(ssTable)==false;    
    outcell = merge_cells(outcell, ssTable, 2);
end
clear mod_est_info_tab estpar_tab_d ss_tab_d datainfo_tab; 
% =========================================================================

% =========================================================================
%% 4.8 Write values of top mode to excel
cd(outpath)
xlsFilename=[load_fname,' ',strdate,'.xls']; 
modeCell=[parnames(:) num2cprec(parMode,15)]; 
if isunix == 0;
    xlswrite(xlsFilename,modeCell,'ModePrecision'); 
elseif isunix == 1; 
    savecell(modecell,[],outpath,xlsFilename);
end
clear modeCell
cd(cucd); 

%% Write top mode with STD and Hessian
%  ===================================
if exist('stdHFD','var')==0; 
    stdHFD=nan(size(stdLoopMat,1),1);
end 

parTemp=nan(length(parMode),3);
parTemp(:,1)=parMode;
parTemp(parposest,2)=stdHFD;
parTemp(parposest,3)=stdLoopMat(:,1);
cd(outpath);
if isunix==0 
    xlswrite(xlsFilename,crtcell(parTemp,parnames,{'Mode','Std FDH','Std Estimation'}),...
        'ModeDetails');
else 
    savecell(crtcell(parTemp,parnames,{'Mode','Std FDH','Std Estimation'}),[],outpath,...
        'ModeDetails'); 
end 
cd(cucd);

tableStru.dataMat=parTemp; 
tableStru.rowLabels=convert2tex(parnames); 
tableStru.colLabels={'Mode','Std FDH','Std Estimation'}; 
tableStru.corner={'Parameter'}; 
tableStru.tableHeader={'Mode and Hessian based stds'}; 

latexStru.printPDF=true; 
latexStru.fileName=['Estimation Report',strdate]; 
latexStru.outPath=outpath; 
latexStru.fileExists=false; 

multiPageTable(tableStru,latexStru); 

%MakeLaTeX_table(parTemp,convert2tex(parnames),{'Mode','Std FDH','Std Estimation'},...
%    {'Parameter'},{'Mode'},'ModeTable',0,outpath); 

%%

save_path=cr_dir(outpath,'top mode'); 
temp      = extrsubf(loadpath, cucd ); 
tempos=strfind(temp,'\'); 
if isempty(tempos); 
    tempos=strfind(temp,'/'); 
end 
% if isunix == 0; 
%     tempos    = findstr(temp,'\'); 
% else
%     tempos    = findstr(temp,'/'); 
% end
root=temp(tempos(1):tempos(2)-1); 
spec=temp(tempos(2):tempos(3)-1); 
subf=temp(tempos(3):end        ); 
outfolder = extrsubf(save_path, loadpath);
topcell = {'root', root;
    'spec', spec;
    'subf', subf;
    'outfolder', outfolder
    'data', data_fname};
cd(outpath); 

cd(outpath);
if isunix==0 &&  size(outcell,2) < 250 
    xlswrite(xlsFilename,outcell,'Estimation Results');    
else
    savecell(outcell,[],outpath,'Est Result');
end
cd(cucd); 
disp('Added Est Results') 
% =========================================================================